package cn.iocoder.yudao.module.pplan.service.serviceequipment;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.distm.dal.dataobject.schedule.ScheduleRecordDO;
import cn.iocoder.yudao.module.distm.dal.mysql.schedule.ScheduleRecordMapper;
import cn.iocoder.yudao.module.pplan.controller.admin.serviceequipment.vo.ServiceEquipmentPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.serviceequipment.vo.ServiceEquipmentSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.app.serviceequipment.vo.AppServiceEquipmentSaveReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.serviceequipment.ServiceEquipmentDO;
import cn.iocoder.yudao.module.pplan.dal.mysql.serviceequipment.ServiceEquipmentMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.SERVICE_EQUIPMENT_NOT_EXISTS;

/**
 * 生产计划-服务用具管理 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class ServiceEquipmentServiceImpl implements ServiceEquipmentService {

    @Resource
    private ServiceEquipmentMapper serviceEquipmentMapper;
    @Resource
    private ScheduleRecordMapper scheduleRecordMapper;

    @Override
    public Long createServiceEquipment(ServiceEquipmentSaveReqVO createReqVO) {
        // 插入
        ServiceEquipmentDO serviceEquipment = BeanUtils.toBean(createReqVO, ServiceEquipmentDO.class);
        serviceEquipmentMapper.insert(serviceEquipment);
        // 返回
        return serviceEquipment.getId();
    }

    @Override
    public void updateServiceEquipment(ServiceEquipmentSaveReqVO updateReqVO) {
        // 校验存在
        validateServiceEquipmentExists(updateReqVO.getId());
        // 更新
        ServiceEquipmentDO updateObj = BeanUtils.toBean(updateReqVO, ServiceEquipmentDO.class);
        serviceEquipmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteServiceEquipment(Long id) {
        // 校验存在
        validateServiceEquipmentExists(id);
        // 删除
        serviceEquipmentMapper.deleteById(id);
    }

    @Override
        public void deleteServiceEquipmentListByIds(List<Long> ids) {
        // 校验存在
        validateServiceEquipmentExists(ids);
        // 删除
        serviceEquipmentMapper.deleteByIds(ids);
        }

    private void validateServiceEquipmentExists(List<Long> ids) {
        List<ServiceEquipmentDO> list = serviceEquipmentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SERVICE_EQUIPMENT_NOT_EXISTS);
        }
    }

    private void validateServiceEquipmentExists(Long id) {
        if (serviceEquipmentMapper.selectById(id) == null) {
            throw exception(SERVICE_EQUIPMENT_NOT_EXISTS);
        }
    }

    @Override
    public ServiceEquipmentDO getServiceEquipment(Long id) {
        return serviceEquipmentMapper.selectById(id);
    }

    @Override
    public PageResult<ServiceEquipmentDO> getServiceEquipmentPage(ServiceEquipmentPageReqVO pageReqVO) {
        return serviceEquipmentMapper.selectPage(pageReqVO);
    }

    @Override
    public Long appCreateServiceEquipment(AppServiceEquipmentSaveReqVO createReqVO) {
        //更新调度生成记录distm_shedule_record表的回收状态
        scheduleRecordMapper.updateById(new ScheduleRecordDO().setId(Long.valueOf(createReqVO.getScheduleRecordId()))
                .setRecycleStatus(1));
        //scheduleRecordId
        // 插入
        ServiceEquipmentDO serviceEquipment = BeanUtils.toBean(createReqVO, ServiceEquipmentDO.class);
        serviceEquipmentMapper.insert(serviceEquipment);
        // 返回
        return serviceEquipment.getId();
    }

}